package offer;

import listnode.ListNode;

public class 链表中倒数第k个节点22 {

    // 双指针
    public ListNode getKthFromEnd(ListNode head, int k) {
        ListNode fast = head;
        ListNode slow = head;
        // 间隔k
        for (int i = 0; i < k; i++) {
            slow = slow.getNext();
        }
        while (fast != null) {
            fast = fast.getNext();
            slow = slow.getNext();
        }
        return slow;
    }
}
